/**
 * Copyright (c) 2005-2012 https://github.com/zhangkaitao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.aiwei.tdjk.sys.user.service;

import com.aiwei.tdjk.common.service.BaseService;
import com.aiwei.tdjk.sys.user.entity.User;
import com.aiwei.tdjk.sys.user.entity.UserStatus;
import com.aiwei.tdjk.sys.user.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


@Service("userService")
public class UserService extends BaseService<User, Long> {

    @Autowired
    private PasswordService passwordService;

    @Autowired
    private UserRepository getUserRepository() {
        return (UserRepository) baseRepository;
    }


    public User findByUsername(String username) {
        if(StringUtils.isEmpty(username)) {
            return null;
        }
        return getUserRepository().findByUsername(username);
    }

    public User findByEmail(String email) {
        if(StringUtils.isEmpty(email)) {
            return null;
        }
        return getUserRepository().findByEmail(email);
    }


    public User findByMobilePhoneNumber(String mobilePhoneNumber) {
        if(StringUtils.isEmpty(mobilePhoneNumber)) {
            return null;
        }
        return getUserRepository().findByMobilePhoneNumber(mobilePhoneNumber);
    }




    public void changePassword(Long[] ids, String newPassword) {
//        UserService proxyUserService = (UserService) AopContext.currentProxy();
        for (Long id : ids) {
            User user = findOne(id);
            changePassword(user, newPassword);
//            UserLogUtils.log(
//                    user.getUsername(),
//                    "changePassword",
//                    "admin user {} change password!", opUser.getUsername());

        }
    }

    public User changePassword(User user, String newPassword) {
        user.randomSalt();
        user.setPassword(passwordService.encryptPassword(user.getUsername(), newPassword, user.getSalt()));
        update(user);
        return user;
    }

    public void changeStatus(Long[] ids, UserStatus newStatus, String reason) {
        for (Long id : ids) {
            User user = findOne(id);
            changeStatus(user, newStatus, reason);

        }
    }

    public User changeStatus(User user, UserStatus newStatus, String reason) {
        user.setStatus(newStatus);
        update(user);
        return user;
    }




    private boolean maybeUsername(String username) {
        if (!username.matches(User.USERNAME_PATTERN)) {
            return false;
        }
        //如果用户名不在指定范围内也是错误的
        if (username.length() < User.USERNAME_MIN_LENGTH || username.length() > User.USERNAME_MAX_LENGTH) {
            return false;
        }

        return true;
    }

    private boolean maybeEmail(String username) {
        if (!username.matches(User.EMAIL_PATTERN)) {
            return false;
        }
        return true;
    }

    private boolean maybeMobilePhoneNumber(String username) {
        if (!username.matches(User.MOBILE_PHONE_NUMBER_PATTERN)) {
            return false;
        }
        return true;
    }





}
